package com.ymatou.app.rec.testcase;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.app.rec.parameter.TagBean;
import com.ymatou.app.rec.service.TagCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;


public class Ts_Tag {
	private static TagBean tagBean;
	private static TagCall tagCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("新首页标签推荐");
	}

	@Before
	public void caseUp() {
		tagBean = new TagBean();
		tagCall = new TagCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(tagCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Tag_001() {
		Logger.start(true,"新首页猜你喜欢模块的标签推荐-验证接口返回公共数据 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "homepage_tag_gl_pro";
			String submodule= "tag_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			tagBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			tagBean.setAppkey("1");
			tagBean.setRequestid(requestid);
			tagBean.setModule(module);
			tagBean.setSubmodule(submodule);
			tagBean.setPlatform(platform);
			tagBean.setLimit("0,10");
			
			tagCall.setData(tagBean);
			tagCall.callService();
			
			JSONArray hits = tagCall.getHits();
			
			Logger.verifyEquals("OK",tagCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(tagCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, tagCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, tagCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyEquals(requestid, tagCall.getCommonInfo("requestid"), "验证requestid是否为所传值");
			Logger.verifyNotNull(tagCall.getCommonInfo("hostip"), "hostip不为空");
			Logger.verifyEquals(sort, tagCall.getCommonInfo("algorithm"), "验证返回排序算法是否为所传方法");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Tag_002() {
		Logger.start(true,"新首页最新好货模块的标签推荐-验证接口返回公共数据 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "homepage_tag_recent_pro";
			String submodule= "tag_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			tagBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			tagBean.setAppkey("1");
			tagBean.setRequestid(requestid);
			tagBean.setModule(module);
			tagBean.setSubmodule(submodule);
			tagBean.setPlatform(platform);
			tagBean.setLimit("0,10");
			
			tagCall.setData(tagBean);
			tagCall.callService();
			
			JSONArray hits = tagCall.getHits();
			
			Logger.verifyEquals("OK",tagCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(tagCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, tagCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, tagCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyEquals(requestid, tagCall.getCommonInfo("requestid"), "验证requestid是否为所传值");
			Logger.verifyNotNull(tagCall.getCommonInfo("hostip"), "hostip不为空");
			Logger.verifyEquals(sort, tagCall.getCommonInfo("algorithm"), "验证返回排序算法是否为所传方法");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Tag_003() {
		Logger.start(true,"新首页猜你喜欢模块的标签推荐-验证接口返回字段值是否齐全，并且有有效值");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "homepage_tag_gl_pro";
			String submodule= "tag_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			tagBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			tagBean.setAppkey("1");
			tagBean.setRequestid(requestid);
			tagBean.setModule(module);
			tagBean.setSubmodule(submodule);
			tagBean.setPlatform(platform);
			tagBean.setLimit("0,10");
			
			tagCall.setData(tagBean);
			tagCall.callService();
			
			Logger.verifyEquals("OK", tagCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = tagCall.getHits();
			
			for(int i=1;i<=hitsAry.length();i++){
				
			 String info = hitsAry.get(i-1).toString();
			 JSONObject infoAry = new JSONObject(info);
			 
			 //获取接口返回的key校验返回字段
			  Logger.verifyEquals(true,infoAry.has("tag") , "验证第"+i+"条记录返回结果中有tag标签这个key");
			  Logger.verifyEquals(true,infoAry.has("name") , "验证第"+i+"条记录返回结果中有name关键字这个key");
			  Logger.verifyEquals(true,infoAry.has("value") , "验证第"+i+"条记录返回结果中有value类目或者品牌值这个key");
			  Logger.verifyEquals(true,infoAry.has("type") , "验证第"+i+"条记录返回结果中有type是类目还是品牌这个key");
			
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Tag_004() {
		Logger.start(true,"新首页最新好货模块的标签推荐-验证接口返回字段值是否齐全 ，并且有有效值");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "homepage_tag_recent_pro";
			String submodule= "tag_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			tagBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			tagBean.setAppkey("1");
			tagBean.setRequestid(requestid);
			tagBean.setModule(module);
			tagBean.setSubmodule(submodule);
			tagBean.setPlatform(platform);
			tagBean.setLimit("0,10");
			
			tagCall.setData(tagBean);
			tagCall.callService();
			
			JSONArray hitsAry = tagCall.getHits();
			
			for(int i=1;i<=hitsAry.length();i++){
				
			 String info = hitsAry.get(i-1).toString();
			 JSONObject infoAry = new JSONObject(info);
			 
			 //获取接口返回的key校验返回字段
			  Logger.verifyEquals(true,infoAry.has("tag") , "验证第"+i+"条记录返回结果中有tag标签这个key");
			  Logger.verifyEquals(true,infoAry.has("name") , "验证第"+i+"条记录返回结果中有name关键字这个key");
			  Logger.verifyEquals(true,infoAry.has("value") , "验证第"+i+"条记录返回结果中有value类目或者品牌值这个key");
			  Logger.verifyEquals(true,infoAry.has("type") , "验证第"+i+"条记录返回结果中有type是类目还是品牌这个key");
			
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Tag_005() {
		Logger.start(true,"新首页猜你喜欢模块的标签推荐-检查接口每条数据的返回值是否重复 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "homepage_tag_gl_pro";
			String submodule= "tag_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			tagBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			tagBean.setAppkey("1");
			tagBean.setRequestid(requestid);
			tagBean.setModule(module);
			tagBean.setSubmodule(submodule);
			tagBean.setPlatform(platform);
			tagBean.setLimit("0,10");
			
			tagCall.setData(tagBean);
			tagCall.callService();
			
			Logger.verifyEquals("OK", tagCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = tagCall.getHits();
			
			List<String> list = new ArrayList<String>();
			List<Integer> listInt = new ArrayList<Integer>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String name = json.getString("name");
				
				if (list.contains(name) == true) {

					Logger.verifyEquals(false, list.contains(name), "验证第" + i+ "条返回数据的name推荐关键字存在重复值" + name);

				}else{
					Logger.verifyEquals(false, list.contains(name), "验证第" + i+ "条返回数据的name推荐关键字不存在重复值" + name);
					list.add(name);
				}
				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Tag_006() {
		Logger.start(true,"新首页最新好货模块的标签推荐-检查接口每条数据的返回值是否重复");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "homepage_tag_recent_pro";
			String submodule= "tag_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			tagBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			tagBean.setAppkey("1");
			tagBean.setRequestid(requestid);
			tagBean.setModule(module);
			tagBean.setSubmodule(submodule);
			tagBean.setPlatform(platform);
			tagBean.setLimit("0,10");
			
			tagCall.setData(tagBean);
			tagCall.callService();
			
			Logger.verifyEquals("OK", tagCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = tagCall.getHits();
			
			List<String> list = new ArrayList<String>();
			List<Integer> listInt = new ArrayList<Integer>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String name = json.getString("name");
				
				if (list.contains(name) == true) {

					Logger.verifyEquals(false, list.contains(name), "验证第" + i+ "条返回数据的name推荐关键字存在重复值" + name);

				}else{
					Logger.verifyEquals(false, list.contains(name), "验证第" + i+ "条返回数据的name推荐关键字不存在重复值" + name);
					list.add(name);
				}
				
			}
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Tag_007() {
		Logger.start(true,"新首页猜你喜欢模块的标签推荐-验证searchInfo里的返回数据 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "homepage_tag_gl_pro";
			String submodule= "tag_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			tagBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			tagBean.setAppkey("1");
			tagBean.setRequestid(requestid);
			tagBean.setModule(module);
			tagBean.setSubmodule(submodule);
			tagBean.setPlatform(platform);
			tagBean.setLimit("0,10");
			
			tagCall.setData(tagBean);
			tagCall.callService();
			
			JSONObject searchInfoJsonObject= (JSONObject) tagCall.getSearchInfo();
			Logger.verifyEquals(true, searchInfoJsonObject.has("module"), "验证searchInfo里是否有返回module模块字段信息");
			Logger.verifyEquals(true, searchInfoJsonObject.has("algo"), "验证searchInfo里是否有返回algo算法字段信息");
			Logger.verifyEquals(true, searchInfoJsonObject.has("yid"), "验证searchInfo里是否有返回yid字段信息");
			Logger.verifyEquals(module, searchInfoJsonObject.get("module"), "验证返回module值");
			Logger.verifyEquals(sort, searchInfoJsonObject.get("algo"), "验证返回algo值");
			Logger.verifyNotNull(searchInfoJsonObject.get("yid"), "验证返回yid不能为空");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Tag_008() {
		Logger.start(true,"新首页最新好货模块的标签推荐-验证searchInfo里的返回数据 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "homepage_tag_recent_pro";
			String submodule= "tag_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			tagBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			tagBean.setAppkey("1");
			tagBean.setRequestid(requestid);
			tagBean.setModule(module);
			tagBean.setSubmodule(submodule);
			tagBean.setPlatform(platform);
			tagBean.setLimit("0,10");
			
			tagCall.setData(tagBean);
			tagCall.callService();
			
			JSONObject searchInfoJsonObject= (JSONObject) tagCall.getSearchInfo();
			Logger.verifyEquals(true, searchInfoJsonObject.has("module"), "验证searchInfo里是否有返回module模块字段信息");
			Logger.verifyEquals(true, searchInfoJsonObject.has("algo"), "验证searchInfo里是否有返回algo算法字段信息");
			Logger.verifyEquals(true, searchInfoJsonObject.has("yid"), "验证searchInfo里是否有返回yid字段信息");
			Logger.verifyEquals(module, searchInfoJsonObject.get("module"), "验证返回module值");
			Logger.verifyEquals(sort, searchInfoJsonObject.get("algo"), "验证返回algo值");
			Logger.verifyNotNull(searchInfoJsonObject.get("yid"), "验证返回yid不能为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Tag_009() {
		Logger.start(true,"新首页猜你喜欢模块的标签推荐-验证接口返回总记录数 ");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "homepage_tag_gl_pro";
			String submodule= "tag_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			int limit = 3;
			
			tagBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			tagBean.setAppkey("1");
			tagBean.setRequestid(requestid);
			tagBean.setModule(module);
			tagBean.setSubmodule(submodule);
			tagBean.setPlatform(platform);
			tagBean.setLimit("0,"+limit+"");
			
			tagCall.setData(tagBean);
			tagCall.callService();
			JSONArray hits = tagCall.getHits();
			int total = tagCall.getTotal();
			
			Logger.verifyEquals(true,total>limit, "检查总记录数是大于当前页总数的");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Tag_010() {
		Logger.start(true,"新首页最新好货模块的标签推荐-验证接口返回总记录数");
		try {
			//设置参数
			String requestid = "1111111";
			String module = "homepage_tag_recent_pro";
			String submodule= "tag_list_md";
			String platform = "appbuyer";
			String sort = "algo1";
			int limit = 3;
			tagBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,sort:"+sort+"");
			tagBean.setAppkey("1");
			tagBean.setRequestid(requestid);
			tagBean.setModule(module);
			tagBean.setSubmodule(submodule);
			tagBean.setPlatform(platform);
			tagBean.setLimit("0,"+limit+"");
			
			tagCall.setData(tagBean);
			tagCall.callService();
			
			JSONArray hits = tagCall.getHits();
			int total = tagCall.getTotal();
			
			Logger.verifyEquals(true,total>limit, "检查总记录数是大于当前页总数的");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
